-- local modeDynamic = 0
-- local modeStatic = 1
-- local mode = 0
-- local replaceBlocks = false
local length = 16
local height = 2
local h = 0
local direction = 0
local wallModeDisabled = 0
local wallModeRight = 1
local wallModeLeft = 2
local wallModeBoth = 3
local wallMode = 3
local floor = true
local ceiling = true
local returnTurtle = true
local removeObstacles = false
local fuelSlot = 1
local leftWallMaterialSlot = 2
local rightWallMaterialSlot = 3
local floorMaterialSlot = 4
local ceilingMaterialSlot = 5
local outOfLeftWallMaterial = "Ran out of left wall material"
local refillLeftWallMaterial = "Refill left wall material and press enter"
local outOfRightWallMaterial = "Ran out of right wall material"
local refillRightWallMaterial = "Refill right wall material and press enter"
local outOfFloorMaterial = "Ran out of floor material"
local refillFloorMaterial = "Refill floor material and press enter"
local outOfceilingMaterial = "Ran out of ceiling material"
local refillceilingMaterial = "Refill ceiling material and press enter"
local refuelingTurtle = "Low fuel detected, refueling"
local outOfFuel = "Turtle out of fuel, waiting for refuel"
local tunnelCompleted = "Tunnel building completed"
local turtleReturning = "Turtle returning to start"
local turtleReturned = "Turtle returned to start position"
local obstacleEncountered = "Obstacle encountered, waiting"
local author = "Kevin Scroggins"
local nickname = "nitro glycerine"
local email = "nitro404@gmail.com
local website = "http://www.nitro404.com"

function checkFuel()
  if turtle.getItemCount(fuelSlot) == 0 then
    print(outOfFuel)
    
    while turtle.getItemCount(fuelSlot) == 0 do
      os.sleep(1)
    end
  end
  
  if turtle.getFuelLevel() < 15 then
    print(refuelingTurtle)
    turtle.select(fuelSlot)
    if turtle.refuel(1) ~= true then
      print(outOfFuel)
      
      while turtle.refuel(1) ~= true do
        os.sleep(1)
      end
    end
    
    checkFuel()
  end
end

function forward(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.forward() ~= true then
      print(obstacleEncountered)
      
      while turtle.forward() ~= true do
        checkFuel()
        
        if removeObstacles == true then
          turtle.dig()
        end
        
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function back(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.back() ~= true then
      print(obstacleEncountered)
      
      while turtle.back() ~= true do
        checkFuel()
        
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function up(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.up() ~= true then
      print(obstacleEncountered)
      
      while turtle.up() ~= true do
        checkFuel()
        
        if removeObstacles == true then
          turtle.digUp()
        end
        
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function down(d)
  if d == nil then
    d = 1
  elseif d < 1 then
    return
  end
  
  for i = 0, d - 1, 1 do
    checkFuel()
    
    if turtle.down() ~= true then
      print(obstacleEncountered)
      
      while turtle.down() ~= true do
        checkFuel()
        
        if removeObstacles == true then
          turtle.digDown()
        end
        os.sleep(0.5)
      end
    end
  end
  
  checkFuel()
end

function left(n)
  if n == nil then
    n = 1
  elseif n < 1 then
    return
  end
  
  for i = 0, n - 1, 1 do
    turtle.turnLeft()
  end
end

function right(n)
  if n == nil then
    n = 1
  elseif n < 1 then
    return
  end
  
  for i = 0, n - 1, 1 do
    turtle.turnRight()
  end
end

function checkLeftWallMaterial()
  while turtle.getItemCount(leftWallMaterialSlot) == 0 do
    print(outOfLeftWallMaterial)
    print(refillLeftWallMaterial)
    
    io.read()
  end
end

function checkRightWallMaterial()
  while turtle.getItemCount(rightWallMaterialSlot) == 0 do
    print(outOfRightWallMaterial)
    print(refillRightWallMaterial)
    
    io.read()
  end
end

function checkFloorMaterial()
  while turtle.getItemCount(floorMaterialSlot) == 0 do
    print(outOfFloorMaterial)
    print(refillFloorMaterial)
    
    io.read()
  end
end

function checkceilingMaterial()
  while turtle.getItemCount(ceilingMaterialSlot) == 0 do
    print(outOfceilingMaterial)
    print(refillceilingMaterial)
    
    io.read()
  end
end

function buildLeftWall()
  if direction == 0 then
    left()
  else
    right()
  end
  
  checkLeftWallMaterial()
  
  turtle.select(leftWallMaterialSlot)
  turtle.place()
  
  checkLeftWallMaterial()
  
  if direction == 0 then
    right()
  else
    left()
  end
end

function buildRightWall()
  if direction == 0 then
    right()
  else
    left()
  end
  
  checkRightWallMaterial()
  
  turtle.select(rightWallMaterialSlot)
  turtle.place()
  
  checkRightWallMaterial()
  
  if direction == 0 then
    left()
  else
    right()
  end
end

function buildFloor()
  checkFloorMaterial()
  
  turtle.select(floorMaterialSlot)
  turtle.placeDown()
  
  checkFloorMaterial()
end

function buildceiling()
  checkceilingMaterial()
  
  turtle.select(ceilingMaterialSlot)
  turtle.placeUp()
  
  checkceilingMaterial()
end

function build()
  if floor == true and h == 0 then
    buildFloor()
  end
  
  if ceiling == true and h == height - 1 then
    buildceiling()
  end
  
  if wallMode == wallModeLeft or wallMode == wallModeBoth then
    buildLeftWall()
  end
  
  if wallMode == wallModeRight or wallMode == wallModeBoth then
    buildRightWall()
  end
end

function returnToStart()
  if direction == 0 then
    right(2)
    
    forward(length - 1)
  end
  
  down(h)
  
  right(2)
end

function buildTunnel()
  forward()
  
  for j = 0, height - 1, 1 do
    for i = 0, length - 1, 1 do
      build()
      if i < length - 1 then
        forward()
      end
    end
    
    if j < height - 1 then
      right(2)
      up()
      
      h = h + 1
      
      if direction == 0 then
        direction = 1
      else
        direction = 0
      end
    end
  end
  
  print(tunnelCompleted)
  
  if returnTurtle == true then
    print(turtleReturning)
    returnToStart()
    print(turtleReturned)
  end
  
  back()
end

buildTunnel()
